#!/usr/bin/env bash

# edit_file.sh

# This program is part of Leah (Language editor and helper).

# Leah is part of Faridayix, an artificially intelligent computer
# operating system.

# Copyright (C) 2010 Scott C. MacCallum
# scottmaccal@gmail.com

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

(define edit_file
{
  clear

  ls --almost-all

  display "\n"

  display "Above is a listing of files in the directory you are currently in."

  display "\n"

  display "\n"

  display "Do you see the file that you would like to edit? "

  echo -e -n $bold_font

  read see_file

  echo -e -n $default_font

    if [[ "$see_file" == "yes" || "$see_file" == "y" ]]; then
      display "\n"

      display "What file would you like to edit? "

      echo -e -n $bold_font

      read file_name

      echo -e -n $default_font

      clear

      display "I will display ${bold_font}$file_name${default_font} shortly."

      display "\n"

      display "\n"

      display "Use the ${reverse_font}${bold_font}up${default_font} and ${reverse_font}${bold_font}down${default_font} arrow keys on you keyboard to move through the file."

      display "\n"

      display "\n"

      display "Input the ${reverse_font}${bold_font}q${default_font} key to continue editing."

      sleep 10

      clear

      nl --body-numbering=a --number-width=1 -s " " $file_name | less

      nl --body-numbering=a --number-width=1 -s " " $file_name

      display "\n"
  
      display "What line would you like to look at? "
  
      echo -e -n $bold_font

      read line_number

      echo -e -n $default_font

      let lines=11

      declare -i file_section

      file_section=$line_number+$lines

      nl --body-numbering=a --number-width=1 -s " " $file_name | head -n $file_section
  
    else
      display "\n"

      echo -e -n $reverse_font

      echo -e -n $bold_font

      display "I do not understand what you said."

      echo -e -n $default_font

      display "\n"

      display "\n"

      echo -e -n "Please answer me with a ${reverse_font}${bold_font}y${default_font}es or ${reverse_font}${bold_font}n$\
{default_font}o."

      echo -e -n $default_font

      sleep 5

      edit_file
    fi

  display "\n"

  display "What file would you like to edit? "

  echo -e -n $bold_font

  read file_name

  echo -e -n $default_font

  display "\n"

  nl --body-numbering=a --number-width=1 -s " " $file_name
  
  display "\n"
  
  display "What line would you like to look at? "
  
  echo -e -n $bold_font

  read line_number

  echo -e -n $default_font
  
  display "\n"
  
  display "Would you like to edit this line or insert an empty line above it? "

  echo -e -n $bold_font
  
  read line_option

  echo -e -n $default_font

  display "\n"
}
#EOF
